<?php
namespace app\common\model;
use \think\Model;

class WapFile extends File{
	
	public function wap_edit_upload($user_id = 0, $file_ext = '', $path = '', $admin_id = 0){
    	$path = $path ? $path : '/uploads/file/';

		$file = request()->post('filed');

		$AS   = explode(',',$file);
		$img  = base64_decode($AS[1]);

		$ext_str = str_replace(array('data:',';base64'),'', $AS[0]);
		$ext_ary = explode('/', $ext_str); 
		$ext     = $ext_ary[1];

		$file_name =  $path . md5(time()) . '.' . $ext;
		
		$size = file_put_contents(ROOT_PATH . 'service' . $file_name, $img);//返回的是字节数
		
		$fileinfo = $file_name;

		if($size){
			$info = pathinfo($fileinfo);
			// 原文件名
			$data['filename']    = $info['basename'];
			// 文件大小
			$data['size']        = $size; 
			// 后缀
			$data['ext']         = $info['extension'];
			// 保存路径
			$data['filepath']    = $file_name;
			$data['file_md5']    = md5($data['filepath']);
			// 保存文件名
			$data['savename']    = $info['basename']; 
			$data['client_ip']   = request()->ip();
			$data['create_time'] = time();
			$data['user_id']     = $user_id ? $user_id : 0;
			$data['admin_id']    = $admin_id ? $admin_id : 0;
			
			$is_pic = $this->getIsPicAttr($data['ext'], $data);
			if($is_pic){
				$img_info = getimagesize('.' . $data['filepath']);
				$data['img_width']  = $img_info[0];
				$data['img_height'] = $img_info[1];
			}
			
			model('File')->insert($data);

			$data['is_pic']   = $is_pic;
			$data['file_url'] = $this->getFileUrlAttr($data['filepath'], $data);
			$data['icon_url'] = $this->getIconUrlAttr($data['filepath'], $data);
			$data['file_id']  = $this->getLastInsID();

			return $data;
		}else{
			// 上传失败获取错误信息
			// $this->tips_info = $file->getError();
			return false;
		}
    }
}